#include <iostream>
#include <string>
#include <fstream>
#include "../../src/config/config_manager.h"
#include "../../src/data/name_list.h"
#include "../../src/ui/console_ui.h"
#include "../../src/utils/platform.h"

class SetupTool {
private:
    config::ConfigManager configManager;
    data::NameList nameList;
    
public:
    void run() {
        initialize();
        
        while (true) {
            showStartScreen();
        }
    }
    
private:
    void initialize() {
        utils::Platform::setUTF8Encoding();
        utils::Platform::createDirectory("data");
        
        // Create necessary files if they don't exist
        std::ofstream("data/namelist.txt", std::ios::app).close();
        std::ofstream("data/config.conf", std::ios::app).close();
        
        configManager.loadFromFile("data/config.conf");
        nameList.loadFromFile("data/namelist.txt");
    }
    
    void showStartScreen() {
        ui::ConsoleUI::showConfigurationMenu();
        std::cout << "如果配置成功且无更改，输入\"exit\"退出，反之按回车";
        std::string input = ui::ConsoleUI::getUserInput();
        
        if (input == "exit") {
            std::cout << "退出配置。" << std::endl;
            #ifdef _WIN32
                system("pause");
            #endif
            exit(0);
        } else {
            std::cout << "继续配置..." << std::endl;
            showMainMenu();
        }
    }
    
    void showMainMenu() {
        ui::ConsoleUI::showConfigurationMenu();
        std::cout << "选择配置项：" << std::endl;
        std::cout << "1. 查看和编辑名单" << std::endl;
        std::cout << "2. 快速添加名单或重新配置人员数量" << std::endl;
        std::cout << "3. 重新配置名单" << std::endl;
        std::cout << "4. 设置点名模式" << std::endl;
        std::cout << "5. 返回" << std::endl;

        int choice = ui::ConsoleUI::getUserChoice(1, 5);
        switch (choice) {
            case 1: viewAndEditNamelist(); break;
            case 2: quickAddNames(); break;
            case 3: reconfigureNames(); break;
            case 4: setPickMode(); break;
            case 5: return; // Return to start screen
            default: break;
        }
    }
    
    void viewAndEditNamelist() {
        ui::ConsoleUI::showConfigurationMenu();
        std::cout << "当前名单：" << std::endl;
        
        if (nameList.isEmpty()) {
            std::cout << "名单为空" << std::endl;
        } else {
            const auto& names = nameList.getNames();
            for (size_t i = 0; i < names.size(); i++) {
                std::cout << (i + 1) << ". " << names[i] << std::endl;
            }
        }
        
        ui::ConsoleUI::waitForEnter();
        showMainMenu();
    }
    
    void quickAddNames() {
        ui::ConsoleUI::showConfigurationMenu();
        std::cout << "快速添加名单..." << std::endl;
        
        std::string name;
        while (true) {
            std::cout << "请输入要添加的姓名：(输入EOF退出并配置人员总量)";
            name = ui::ConsoleUI::getUserInput();
            
            if (name == "EOF") {
                break;
            }
            
            if (!name.empty()) {
                nameList.addName(name);
                std::cout << "已添加: " << name << std::endl;
            }
        }
        
        updateNameListAndConfig();
        showMainMenu();
    }
    
    void reconfigureNames() {
        ui::ConsoleUI::showConfigurationMenu();
        std::cout << "重新配置名单..." << std::endl;
        
        nameList.clear();
        
        std::string name;
        while (true) {
            std::cout << "请输入要添加的姓名：(输入EOF退出并配置人员总量)";
            name = ui::ConsoleUI::getUserInput();
            
            if (name == "EOF") {
                break;
            }
            
            if (!name.empty()) {
                nameList.addName(name);
                std::cout << "已添加: " << name << std::endl;
            }
        }
        
        updateNameListAndConfig();
        showMainMenu();
    }
    
    void setPickMode() {
        ui::ConsoleUI::showConfigurationMenu();
        std::cout << "选择点名模式：" << std::endl;
        std::cout << "1. 全局随机" << std::endl;
        std::cout << "2. 逐个随机" << std::endl;
        std::cout << "3. 返回" << std::endl;

        int modeChoice = ui::ConsoleUI::getUserChoice(1, 3);
        
        if (modeChoice == 3) {
            showMainMenu();
            return;
        }
        
        config::PickMode mode = (modeChoice == 1) ? config::PickMode::ALL_RANDOM : config::PickMode::ONE_BY_ONE;
        configManager.setMode(mode);
        configManager.saveToFile("data/config.conf");
        
        std::cout << "点名模式已设置为: " << configManager.getModeDescription() << std::endl;
        ui::ConsoleUI::waitForEnter();
        showMainMenu();
    }
    
    void updateNameListAndConfig() {
        if (nameList.saveToFile("data/namelist.txt")) {
            configManager.setNameCount(nameList.getCount());
            configManager.saveToFile("data/config.conf");
            std::cout << "名单中共有 " << nameList.getCount() << " 个姓名。" << std::endl;
        } else {
            std::cout << "保存名单失败。" << std::endl;
        }
        ui::ConsoleUI::waitForEnter();
    }
};

int main() {
    SetupTool setup;
    setup.run();
    return 0;
}